home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 7259 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.2 KB

  1. Path: pegasus.montclair.edu!harmon
  2. From: harmon@pegasus.montclair.edu (Derek Harmon)
  3. Newsgroups: comp.lang.c,alt.bbs.proboard
  4. Subject: Re: Needed: Centering Procedure that ignores escape sequences
  5. Date: 13 Feb 1996 17:14:47 -0500
  6. Organization: Montclair State University
  7. Message-ID: <harmon.824246552@pegasus.montclair.edu>
  8. References: <4fog4q$24v@news.mountain.net>
  9. NNTP-Posting-Host: pegasus.montclair.edu
  10. X-Newsreader: NN version 6.5.0 #68 (NOV)
  11.  
  12. matt.jenkins@westvirginia.com (Light Chaser) writes:
  13.  
  14. >I spent a whole day trying to come up with a simple and small
  15. >centering routine.  I came up with one, but then ran into the problem
  16. >that I have to ignore escape sequences.  
  17.  
  18. >center("\n\2This \3text \5contains \1escape \6sequences\t\n");
  19.  
  20.    I take it a permissible solution would be to, within center() on a local
  21. copy of the string argument, remove the escape sequences.  It appears all
  22. escape sequences are 2-bytes.  A slight refinement will be necessary to this
  23. code if "\\" is to be allowed to remain as "\", but that should be straight-
  24. forward.
  25.  
  26. : #define MAXLEN 80
  27. : #define ESCAPE '\\'
  28. :
  29. : int center(const char *line)
  30. : {
  31. :    char s[MAXLEN];  /* 80 or any other max display line length will do */
  32. :    int leng;        /* length of string s */
  33. :    int i, j;        /* position within string and general indicies */
  34. :
  35. :    strcpy( s, line);  /* s <- copy of line, line will not be modified */
  36. :    i = 0;
  37. :    leng = strlen( s );
  38. :
  39. :    while( i < leng )         /* when i == leng, s is stripped of escapes */
  40. :        if ( s[i] == ESCAPE ) {  /* this can be any leading escape char */
  41. :            for( j = i; (j + 2) <= leng; j++ )
  42. :                s[j] = s[(j + 2)];  /* overwrite by shifting forward */
  43. :            leng -= 2;              /* length is now 2 shorter */
  44. :        }
  45. :        else i++; /* when length not 2 shorter, process 1 position further */
  46. :    s[(leng + 1)] = '\0';  /* housekeeping, nul was never advanced */
  47. :    
  48. :    i = MAXLEN / 2 - leng / 2;  /* number of leading spaces to print */
  49. :    for( j = 0; j < i; j++ ) fputchar(' ');  /* stdout is assumed */
  50. :    for( j = 0; j < leng; j++ ) fputchar(s[j]);  /* more eff. than printf() */
  51. :    fputchar('\n');             /* print newline for good measure */
  52. :
  53. :    return( leng + i + 1 );     /* return number of chars printed */
  54. : }                              /* just as easy to return stripped str leng */
  55.  
  56.    You can invoke it as center("\\3Hello\\3!"); or strcpy(svar, "\\3Hello!");
  57. and then center(svar); but it is significant in C that when you create any
  58. strings including the backslash as an escape character (since it is also an
  59. escape character in the C language) that you repeat it twice.  '\\' is a
  60. backslash, '\3' is a character with ASCII value 3.  If you can't do that in
  61. your case, another function to change backslashes to asterisks or double
  62. them before hand may be necessary.  Note that strlen("\\ABC\\"); is 5, not
  63. 7.
  64.                                               -- Stone
  65. --
  66. # Derek Harmon (aka Stonelight)    harmon@pegasus.montclair.edu
  67. # - Computer Science Undergrad, Montclair State University, NJ
  68. # - My views are my own, nobody else is this creative.  3;)>
  69. ... C programmer run, C programmer crash, Ada programmer.
  70.  
  71.